package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Author:shixiaojun@itany.com
 * Date: 2022/3/21 11:04
 * Version: 1.0-SNAPSHOT
 */
@Api(tags = "用户功能模块相关的Restful API接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

//    public static final String GLOBAL_URL = "http://localhost:8001/users";
    public static final String GLOBAL_URL = "http://localhost:9001/users";

    @ApiOperation(value = "这是查询所有用户功能的接口",notes = "备注:该方法最终被张三进行了修改")
    @GetMapping
    public ResponseResult findAll(){
        // http://localhost:8001/users
        return restTemplate.getForObject(GLOBAL_URL,ResponseResult.class);
    }

    @ApiOperation("这是根据主键查询指定用户功能的接口")
    @ApiImplicitParam(name = "id",value = "id,主键字段",dataType = "Integer")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        // http://localhost:8001/users/{id}
        // 当前请求中如果存在参数,可以使用{xxx}的方式进行参数的传递
        // xxx的值理论上是可以随便写的
        // 一般情况下,我们会使用当前请求对应的变量名表示
        // 在为参数传值的时候,是根据参数的顺序一一对应的
        // 类似于JDBC中的?传参
//        return restTemplate.getForObject(GLOBAL_URL+"/{aa}",ResponseResult.class,id);
        return restTemplate.getForObject(GLOBAL_URL+"/{id}",ResponseResult.class,id);
    }

    @ApiOperation("这是添加指定用户功能的接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "id,主键字段",dataType = "Integer"),
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "phone",value = "手机号",required = true),
            @ApiImplicitParam(name = "address",value = "地址",required = true)
    })
    @PostMapping
    public ResponseResult addUser(User user){
       // http://localhost:8001/users
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("username",user.getUsername());
        params.add("password",user.getPassword());
        params.add("phone",user.getPhone());
        params.add("address",user.getAddress());
        return restTemplate.postForObject(GLOBAL_URL,params,ResponseResult.class);
    }

    @ApiIgnore
    @ApiOperation("这是根据主键删除指定用户功能的接口")
    @ApiImplicitParam(name = "id",value = "id,主键字段",dataType = "Integer",required = true)
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Integer id){
        // 在restTemple中提供了与Delete请求和Put请求对应的方法
        // 但是这两个方法并没有提供返回值
        // 但是消费者在调用时需要返回值
        // 因此,此处这两个方法不适用
        // 可以使用restTemple提供的通用方法exchange进行实现
        // 该方法适用于任意请求
//        restTemplate.delete();
//        restTemplate.put();
        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(GLOBAL_URL + "/{id}", HttpMethod.DELETE, null, ResponseResult.class, id);
        return responseEntity.getBody();
    }

    @ApiOperation("这是修改指定用户功能的接口")
    @PutMapping
    public ResponseResult modifyUser(User user){
        String url = new StringBuffer()
                .append(GLOBAL_URL)
                .append("?id={id}")
                .append("&username={username}")
                .append("&password={password}")
                .append("&phone={phone}")
                .append("&address={address}")
                .toString();
        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, null, ResponseResult.class, user.getId(), user.getUsername(), user.getPassword(), user.getPhone(), user.getAddress());
        return responseEntity.getBody();
    }

}
